Macromedia Flex Article

Learning Flex Basics (Part 1): Creating Your First Flex Application


Table of Contents

  1. Introduction
  2. A Brief Overview of Macromedia Flex
  3. Building the Application
  4. Create the Application Object
  5. Add a Panel for the Application Layout
  6. Add a Label for the View Header
  7. Add an Array of Objects to Define the Coffee Blends
  8. Add a ComboBox to Display the Coffee Blends
  9. Add a Text Control to Display the Description
  10. Add a Button to Add a Coffee Blend to the Shopping Cart
  11. Add a List Control for the Shopping Cart
  12. Add a Script to Handle the Button Click Event
  13. Complete Code Listing for firstapp.mxml
  14. Where to Go From Here

A Brief Overview of Macromedia Flex

Macromedia Flex is a server component that enables you to create rich presentation layers for Internet applications. Interfaces built in Flex rely on Macromedia Flash Player for display on client systems. The essential components of Flex are:

  • An XML language (MXML) used to describe the application interface.
  • An ECMA scripting language (ActionScript) used with MXML to handle user and system events or to construct complex data models.
  • A class library
  • Runtime services
  • A compiler that generates SWF files from MXML files.

MXML

MXML is an XML 1.0 language used to describe Flex presentation layers. Every MXML file should begin with an XML declaration:

<?xml version="1.0"?>

Like other XML languages, MXML includes elements (coded as tags) and attributes whose names are case-sensitive. Element names begin with a capital letter and use mixed case, and you must close all elements:

<ComboBox></ComboBox>

You may also close elements that have no contents with an ending /:

<ComboBox/>

Attributes begin with lowercase and use mixed case. You must enclose all values for attributes in quotation marks.

<ComboBox id="myCombo"/>

For all attributes except events like click or initialize, attribute values are assumed to be literal strings by the compiler. To bind data and force the compiler to evaluate an expression, you can wrap all or part of the value in curly braces:

<ComboBox dataProvider="{myArray}"/>

You can write most attributes as subtags with values written as contents:

<ComboBox dataProvider="{myArray}"/>

Or:

<ComboBox> <dataProvider>{myArray}</dataProvider> </ComboBox>

MXML files that describe full applications will have an Application element that encloses all other elements:

<?xml version="1.0"?>  
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
  [other elements...]  
</mx:Application>

Note the xmlns attribute, which declares an XML namespace. Namespaces let you to use multiple XML languages in a single document without confusion between identical element names that are used differently in different languages. The :mx here defines a prefix used for a particular namespace.

Note: The namespace declaration here is a standard one for the MXML class library; include it in every MXML file.

Place the declaration in any MXML tag; doing this makes it available to all elements nested inside that tag. Using the mx prefix for the MXML class library is a recommended practice that this tutorial series will follow throughout these tutorials.

For more information, consult the Flex Language Reference.

ActionScript

ActionScript is an object-oriented scripting language similar to JavaScript and other ECMA scripting languages. If you have used JavaScript or object-oriented programming languages like Java or C#, you will find most of ActionScript syntax familiar. You can embed ActionScript code directly in MXML or import it from separate files.

Find a complete reference to ActionScript in the ActionScript Language Reference.

The MXML Class Library

Flex includes an extensive class library for both visual components such as controls and containers, and faceless components such as remote service objects and data models. You will look at many of these in some detail in the tutorials.

Runtime Services

Flex provides several runtime services such as history management and access of remote service objects. From a development perspective, these services are invoked transparently through the class library.

Compiler

The Flex compiler automatically compiles the corresponding SWF when the browser requests an MXML file. The SWF file is automatically cached until you modify the source files.